package com.micro.ai.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.auth.entity.DictData;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 字典数据Mapper接口
 *
 * @author micro-ai
 * @since 2025-01-29
 */
@Mapper
public interface DictDataMapper extends BaseMapper<DictData> {

    /**
     * 分页查询字典数据
     *
     * @param page 分页参数
     * @param dictTypeId 字典类型ID
     * @param label 字典标签
     * @param value 字典值
     * @param status 状态
     * @return 分页结果
     */
    @Select("<script>" +
            "SELECT dd.*, dt.code as dict_type_code, dt.name as dict_type_name " +
            "FROM dict_data dd " +
            "LEFT JOIN dict_types dt ON dd.dict_type_id = dt.id " +
            "WHERE dd.deleted_at IS NULL " +
            "<if test='dictTypeId != null and dictTypeId != \"\"'>" +
            "AND dd.dict_type_id = #{dictTypeId} " +
            "</if>" +
            "<if test='label != null and label != \"\"'>" +
            "AND dd.label LIKE CONCAT('%', #{label}, '%') " +
            "</if>" +
            "<if test='value != null and value != \"\"'>" +
            "AND dd.value LIKE CONCAT('%', #{value}, '%') " +
            "</if>" +
            "<if test='status != null and status != \"\"'>" +
            "AND dd.status = #{status} " +
            "</if>" +
            "ORDER BY dd.sort_order ASC, dd.created_at ASC" +
            "</script>")
    IPage<DictData> selectDictDataPage(Page<DictData> page,
                                       @Param("dictTypeId") String dictTypeId,
                                       @Param("label") String label,
                                       @Param("value") String value,
                                       @Param("status") String status);

    /**
     * 根据字典类型ID查询字典数据
     *
     * @param dictTypeId 字典类型ID
     * @return 字典数据列表
     */
    @Select("SELECT * FROM dict_data WHERE dict_type_id = #{dictTypeId} AND deleted_at IS NULL " +
            "ORDER BY sort_order ASC, created_at ASC")
    List<DictData> selectByDictTypeId(@Param("dictTypeId") String dictTypeId);

    /**
     * 根据字典类型编码查询字典数据
     *
     * @param dictTypeCode 字典类型编码
     * @param tenantId 租户ID
     * @return 字典数据列表
     */
    @Select("SELECT dd.* FROM dict_data dd " +
            "LEFT JOIN dict_types dt ON dd.dict_type_id = dt.id " +
            "WHERE dt.code = #{dictTypeCode} AND dd.status = 'active' AND dd.deleted_at IS NULL " +
            "AND (dt.tenant_id = #{tenantId} OR dt.tenant_id IS NULL) " +
            "ORDER BY dd.sort_order ASC, dd.created_at ASC")
    List<DictData> selectByDictTypeCode(@Param("dictTypeCode") String dictTypeCode, @Param("tenantId") String tenantId);

    /**
     * 根据字典类型编码和值查询字典数据
     *
     * @param dictTypeCode 字典类型编码
     * @param value 字典值
     * @param tenantId 租户ID
     * @return 字典数据
     */
    @Select("SELECT dd.* FROM dict_data dd " +
            "LEFT JOIN dict_types dt ON dd.dict_type_id = dt.id " +
            "WHERE dt.code = #{dictTypeCode} AND dd.value = #{value} AND dd.deleted_at IS NULL " +
            "AND (dt.tenant_id = #{tenantId} OR dt.tenant_id IS NULL) " +
            "ORDER BY dt.tenant_id DESC LIMIT 1")
    DictData selectByDictTypeCodeAndValue(@Param("dictTypeCode") String dictTypeCode,
                                          @Param("value") String value,
                                          @Param("tenantId") String tenantId);

    /**
     * 查询所有启用的字典数据
     *
     * @param tenantId 租户ID
     * @return 字典数据列表
     */
    @Select("SELECT dd.* FROM dict_data dd " +
            "LEFT JOIN dict_types dt ON dd.dict_type_id = dt.id " +
            "WHERE dd.status = 'active' AND dd.deleted_at IS NULL " +
            "AND dt.status = 'active' AND dt.deleted_at IS NULL " +
            "AND (dt.tenant_id = #{tenantId} OR dt.tenant_id IS NULL) " +
            "ORDER BY dd.sort_order ASC, dd.created_at ASC")
    List<DictData> selectActiveDictData(@Param("tenantId") String tenantId);

    /**
     * 根据ID直接查询字典数据（不应用逻辑删除过滤，用于物理删除前检查）
     *
     * @param id 字典数据ID
     * @return 字典数据
     */
    @Select("SELECT * FROM dict_data WHERE id = #{id}")
    DictData selectByIdIgnoreLogic(@Param("id") String id);

    /**
     * 物理删除字典数据（直接执行 DELETE SQL，不应用逻辑删除）
     *
     * @param id 字典数据ID
     * @return 删除的行数
     */
    @Delete("DELETE FROM dict_data WHERE id = #{id}")
    int deleteByIdPhysical(@Param("id") String id);
}
